package lib.operation;

import java.sql.*;

/**
 * @author FBI
 * 2022-02-06 18:22
 */

//查找全部图书
public class SelectAll implements Do{

    @Override
    public void work() {
        SelectAll.selectAllBook();
    }


    public static void selectAllBook(){
        Statement statement = null;
        Connection connection = null;
        ResultSet resultSet = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/lib","root","2506642408");
            statement = connection.createStatement();
            resultSet = statement.executeQuery("select * from book");
            while (resultSet.next()){
                System.out.println("书名:"+resultSet.getString("name") + ", 作者:"+resultSet.getString("author")
                        +", 价格:"+resultSet.getString("price")+"元, 类型:"+resultSet.getString("type"));
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (resultSet != null){
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (statement != null){
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
